import unittest
import  datetime

from Autotests.BaseTest import BaseTestCase


# In Cloud UI Menu/Finance/Generate Invoices.
from Autotests.Cloud.Clients import FinancialService


class Invoices(BaseTestCase):
    def test_Invoices(self):
        finserv = FinancialService()

        def GenerateInvoicesRequest(partnerInvoiceType, billGeneration):
            r = finserv.createObject('GenerateInvoicesRequest')
            r.partnerId = 1
            r.invoicePeriod.fromTime = (datetime.datetime.utcnow() + datetime.timedelta(-30)).isoformat()
            r.invoicePeriod.untilTime = datetime.datetime.utcnow().isoformat()
            r.partnerInvoiceType = 'Intermediate'
            r.invoiceFormat = 'Pdf'
            r.billGeneration = 'DirectDebitor'
            r.storeInDatabase = False

            result = finserv.call('EnumerateInvoiceNumberSequences')

            for i in result.item:
                invNumber = finserv.createObject('InvoiceNumber')
                invNumber.invoicerId = i.id
                invNumber.startInvoiceNumber = 1

                r.startInvoiceNumbers.item.append(invNumber)

            result = finserv.call('GenerateInvoices', r)

        GenerateInvoicesRequest(partnerInvoiceType = 'Intermediate', billGeneration = 'DirectDebitor')
        GenerateInvoicesRequest(partnerInvoiceType = 'Periodic', billGeneration = 'DirectDebitorChildren')

        r = finserv.createObject('ExportInvoicesRequest')
        r.invoicePeriod.fromTime = (datetime.datetime.utcnow() + datetime.timedelta(-30)).isoformat()
        r.invoicePeriod.untilTime = datetime.datetime.utcnow().isoformat()
        r.exportFormat = 'SpreadSheet'

        result = finserv.call('ExportInvoices', r)

        r = finserv.createObject('EnumerateInvoicesRequest')
        r.parentInvoiceId = 1
        r.invoiceType = 'Intermediate'
        r.invoicePeriod.fromTime = (datetime.datetime.utcnow() + datetime.timedelta(30)).isoformat()
        r.invoicePeriod.untilTime = datetime.datetime.utcnow().isoformat()

        finserv.call('EnumerateInvoices', r)

        r = finserv.createObject('EnumerateInvoicesByPeriodRequest')
        r.partnerId = 1
        r.partnerInvoiceType = 'Intermediate'
        r.billingPeriod.startTime = (datetime.datetime.utcnow() + datetime.timedelta(30)).isoformat()
        r.billingPeriod.endTime = datetime.datetime.utcnow().isoformat()

        finserv.call('EnumerateInvoicesByPeriod', r)

        r = finserv.createObject('EnumerateInvoiceTotalsByPeriodRequest')
        r.invoiceType = 'Intermediate'
        r.invoicePeriod.fromTime = (datetime.datetime.utcnow() + datetime.timedelta(30)).isoformat()
        r.invoicePeriod.untilTime = datetime.datetime.utcnow().isoformat()

        finserv.call('EnumerateInvoiceTotalsByPeriod', r)

        r = finserv.createObject('EnumeratePartnerOwnCostByPeriodRequest')
        r.partnerId = 1
        r.partnerInvoiceType = 'Intermediate'
        r.billingPeriod.startTime = (datetime.datetime.utcnow() + datetime.timedelta(30)).isoformat()
        r.billingPeriod.endTime = datetime.datetime.utcnow().isoformat()
        r.currency = 'EUR'
        r.filter = ''

        #finserv.call('EnumeratePartnerOwnCostByPeriod', r) TODO: is this method necessary?

if __name__ == "__main__":
    unittest.main()